package sfeir.ergosum;

import sfeir.ergosum.menu.MenuAction;
import sfeir.ergosum.model.Group;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.sfeir.proto.hivelayout.HiveMenuActionCell;
import com.sfeir.proto.hivelayout.HiveMenuView;

public class HiveMenuGroupActivity extends Activity {

    private HiveMenuView hmv;

    private Thread mThread;
    private Handler mHandler = new Handler();

    final Runnable setupHiveMenu = new Runnable() {
        public void run() {
            hmv.setup();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        hmv = new HiveMenuView(this);
        hmv.setBackgroundColor(0x40000000);
        //hmv.setBackgroundResource(R.drawable.bg);

        final Group cellGroup = (Group) getIntent().getSerializableExtra("group");

        final MenuAction actionRename = new MenuAction(getString(R.string.main_context_menu_rename)) {
            public void executeAction() {
                final Group group = (Group) getObjects().get("group");

                LayoutInflater factory = LayoutInflater.from(HiveMenuGroupActivity.this);
                final View textEntryView = 
                    factory.inflate(R.layout.input_text_dialog, null);
                final TextView textInstruction = 
                    (TextView) textEntryView.findViewById(R.id.instruction_dialog);
                final EditText text = 
                    (EditText) textEntryView.findViewById(R.id.input_dialog);

                textInstruction.setText(R.string.rename_instruction);
                text.setText(group.getName());

                AlertDialog dialog =  new AlertDialog.Builder(HiveMenuGroupActivity.this)
                .setTitle(R.string.rename)
                .setView(textEntryView)
                .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        /* User clicked OK so do some stuff */
                        group.setName(text.getText().toString());
                        ContactManagerActivity.updateGroup(HiveMenuGroupActivity.this, group);
                        HiveMenuGroupActivity.this.finish();
                    }
                }).setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        /* User clicked cancel so do some stuff */
                    }
                }).create();
                dialog.show();
            }
        };
        actionRename.getObjects().put("group", cellGroup);

        final MenuAction actionManageContacts = new MenuAction(getString(R.string.manage_contacts)) {
            public void executeAction() {
                final Group group = (Group) getObjects().get("group");
                Intent intent = new Intent(HiveMenuGroupActivity.this, ContactManagerActivity.class);
                intent.putExtra("groupId", group.getId());
                intent.putExtra("groupName", group.getName());
                HiveMenuGroupActivity.this.startActivity(intent);
                HiveMenuGroupActivity.this.finish();
            }
        };
        actionManageContacts.getObjects().put("group", cellGroup);

        final MenuAction actionDelete = new MenuAction(getString(R.string.main_context_menu_delete)) {
            public void executeAction() {
                final Group group = (Group) getObjects().get("group");

                AlertDialog dialog =  new AlertDialog.Builder(HiveMenuGroupActivity.this)
                .setTitle(R.string.delete_group_confirmation)
                .setIcon(R.drawable.alert_dialog_icon)
                .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        /* User clicked OK so do some stuff */
                        ContactManagerActivity.deleteGroup(HiveMenuGroupActivity.this, group);
                        HiveMenuGroupActivity.this.finish();
                    }
                }).setNegativeButton(R.string.dialog_cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        /* User clicked cancel so do some stuff */
                    }
                }).create();
                dialog.show();
            }
        };
        actionDelete.getObjects().put("group", cellGroup);

        mThread = new Thread() {

            public void run() {
                Looper.prepare();

                HiveMenuActionCell c6 = new HiveMenuActionCell(HiveMenuGroupActivity.this, actionRename);
                c6.setShown(true);
                c6.setCellColor(0xEEB9FF2D);
                c6.setEmboss(true);
                hmv.setHiveMenuCell6(c6);

                HiveMenuActionCell c2 = new HiveMenuActionCell(HiveMenuGroupActivity.this, actionManageContacts);
                c2.setShown(true);
                c2.setCellColor(0xEEB9FF2D);
                c2.setEmboss(true);
                hmv.setHiveMenuCell2(c2);
                
                HiveMenuActionCell c4 = new HiveMenuActionCell(HiveMenuGroupActivity.this, actionDelete);
                c4.setShown(true);
                c4.setCellColor(0xEEB9FF2D);
                c4.setEmboss(true);
                hmv.setHiveMenuCell4(c4);

                MenuAction actionCancel = new MenuAction(cellGroup.getName()) {
                    public void executeAction() {
                        finish();
                    };
                };
                HiveMenuActionCell cs = new HiveMenuActionCell(HiveMenuGroupActivity.this, actionCancel);
                cs.setShown(true);
                cs.setCellColor(0xEE1F98DE);
                cs.setEmboss(true);
                hmv.setHiveStatusMenuCell(cs);

                do {

                } while (!hmv.isConstantsOk());
                mHandler.post(setupHiveMenu);
                Looper.loop();

            }
        };
        mThread.start();

        setContentView(hmv);
    }
}
